package com.niit.onlinemusicserver.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.niit.onlinemusicserver.mapper.RecommendMapper;
import com.niit.onlinemusicserver.model.domain.Recommend;
import com.niit.onlinemusicserver.service.RecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
public class RecommendServiceImpl extends ServiceImpl<RecommendMapper, Recommend> implements RecommendService {

    @Autowired
    private RecommendMapper recommendMapper;

    /**
     * @Description: 向推荐表中写入推荐数据
     * @param: user2songRecMatrix 其中 key 为用户id，value 为推荐给该用户的歌曲id数组。
     */
    @Transactional
    @Override
    public void updatePersonalRecInfo(Map<Integer, Integer[]> user2songRecMatrix) {
        // 先根据用户id 删除该用户在推荐表中的所有历史数据,再批量插入推荐给该用户的歌曲数据；
        user2songRecMatrix.forEach((userId, songIds) -> {
            recommendMapper.deleteByUserId(userId);
            recommendMapper.batchInsert(userId, songIds);
        });
    }
}